package com.put.mybills.activities;

import com.put.mybills.R;
import com.put.mybills.adapter.GlobalParams;
import com.put.mybills.model.Facture;

import android.os.Bundle;
import android.provider.Settings.Global;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;

public class FactureSettings extends Fragment {

	
	String factureName;
	private Facture facture;
	private EditText facutreDescription;
	private CheckBox facturePopUpNotyfication;
	//private CheckBox factureEmailNotyfication;
	private NumberPicker factureRemeinderInterval;
	private NumberPicker facturePaymentInterval;
	
	public FactureSettings(String factureName) {
		this.factureName = factureName;
	}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
		
		
		View rootView = inflater.inflate(R.layout.fragment_facture_settings, container, false);
	      
		facutreDescription = (EditText) rootView.findViewById(R.id.new_facture_description);		
		facturePopUpNotyfication = (CheckBox) rootView.findViewById(R.id.new_facture_pushup_notyfication_chexbox);
		//factureEmailNotyfication = (CheckBox) rootView.findViewById(R.id.new_facture_email_notyfication_chexbox);
		//factureRemeinderInterval = (NumberPicker) rootView.findViewById(R.id.facture_noty_remainder);
		facturePaymentInterval = (NumberPicker) rootView.findViewById(R.id.new_facture_interval);
		
		facture = GlobalParams.getInstance().facturedatasource.getFacture(factureName);
		
		facutreDescription.setText(facture.getDescripiton());
		facturePopUpNotyfication.setChecked(facture.getIsPopUpNotyfication() == 0 ? true : false);
		//factureEmailNotyfication.setChecked(facture.getIsEmalNotyfication() == 0 ? true : false);
	
		
		facturePaymentInterval.setMaxValue(100);       
		facturePaymentInterval.setMinValue(0);        
	    facturePaymentInterval.setWrapSelectorWheel(true);
		facturePaymentInterval.setValue(facture.getInterval());
		facturePaymentInterval.setOnValueChangedListener(new OnValueChangeListener() {
			
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
				picker.setValue(newVal);
				facture.setInterval(newVal);
				GlobalParams.getInstance().facturedatasource.modifyFactue(facture);
				
			}
		});
		
		facturePopUpNotyfication.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {				
				facture.setIsPopUpNotyfication(isChecked ? 0 : 1);
				GlobalParams.getInstance().facturedatasource.modifyFactue(facture);
				
			}
		});
		
		
		/*factureEmailNotyfication.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {				
				facture.setIsEmalNotyfication(isChecked ? 0 : 1);
				GlobalParams.getInstance().facturedatasource.modifyFactue(facture);
				
			}
		});
		*/
		
		
		facutreDescription.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				String des = facutreDescription.getText().toString();
				facture.setDescripiton(des);
				GlobalParams.getInstance().facturedatasource.modifyFactue(facture);
				
				
			}
		});
		
		
		return rootView;
 

	}
	
	
	
	

}
